
/*-----------------------------------------------------------------------------
 * HEADER FILES
 *---------------------------------------------------------------------------*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "gmi_common.h"
#include "gmi_log.h"
#include "gmi_spi.h"
#include "gmi_time.h"

gmi_time_t *h_time = NULL;

/*-----------------------------------------------------------------------------
 * MAIN FUNCTION
 *---------------------------------------------------------------------------*/
int main()
{
    // 创建实例
    gmi_time_t* h_time = gmi_time_init();
    gmi_ts_ms_t timestamp = 0;

    gmi_log_set_level(0);
    FILE* fp = NULL;
    fp       = fopen("./gmi.log", "w+");
    CHECK_STATUS(gmi_log_add_fp(fp, 0));

    // 获取第一次时间戳
    CHECK_STATUS(gmi_get_tick(h_time, &timestamp));
    gmi_log_info("timestamp = %12llu", timestamp);
    CHECK_STATUS(gmi_get_real_timestamp(h_time));
    gmi_log_info("%s", h_time->str_real_time);

    // 延时特定时间
    CHECK_STATUS(gmi_sleep_us(h_time, 1000000));
    // gmi_sleep_ms(h_time, 10);
    // gmi_sleep_s(h_time, 5);

    // 获取第二次时间戳
    CHECK_STATUS(gmi_get_tick(h_time, &timestamp));
    gmi_log_info("timestamp = %12llu", timestamp);
    CHECK_STATUS(gmi_get_real_timestamp(h_time));
    gmi_log_info("%s", h_time->str_real_time);

    GMI_SPI spi1 = {.clock_speed = 10000000,
                    .cpha        = GMI_SPI_CPHA_2ND_EDGE,
                    .cpol        = GMI_SPI_CPOL_HIGH,
                    .crc_enable  = false,
                    .cs_enable   = false};

    CHECK_STATUS(gmi_spi_init(&spi1));

    uint8_t data_buffer[10] = {0x00, 0x02, 0x04, 0x06, 0x0A, 0x0B, 0x0C, 0x0D};
    CHECK_STATUS(gmi_spi_transmit(&spi1, data_buffer, 1));

    // 销毁实例
    gmi_spi_deinit(&spi1);
    gmi_time_deinit(h_time);
    fclose(fp);

    return 0;
}
